//
//  SNFootViewController.m
//  project
//
//  Created by 黄建华 on 2018/11/8.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "SNFoodViewController.h"
#import "SNFoodMealTableViewCell.h"
#import "SNFoodEditViewController.h"
#import "FinishDayPickerView.h"

@interface SNFoodViewController ()

@end

@implementation SNFoodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.leftButton.hidden = NO;
    [self.leftButton setImageNormal:@"进入"];
    self.sbTitleLable.text = SN_Text(@"记录吃了什么");
    self.sbTitleLable.adjustsFontSizeToFitWidth = YES;
    
//    SNShadowView *shadowLine = [[SNShadowView alloc] init];
//    shadowLine.frame = SNFRAME(0, 20+FitSimpleIpx(0, 20) + 44, HHHWIDTH, 0);
//    [shadowLine showWithType:1];
//    [self.view addSubview:shadowLine];
    UIImageView *shadowLine = [[UIImageView alloc] init];
    shadowLine.frame = SNFRAME(0, 20+FitSimpleIpx(0, 20) + 44 -2, HHHWIDTH, 2);
    shadowLine.image = [UIImage image:@"lineImage"];
    [self.view addSubview:shadowLine];
    
    _dataArray = [NSMutableArray array];
    [_dataArray addObjectsFromArray:SN_PublicClass.todayFood.array];
    _tableView                = [[UITableView alloc] initWithFrame:SNFRAME(0, shadowLine.bottom+2, HHHWIDTH, HHHHEIGHT*0.60) style:UITableViewStylePlain];
    _tableView.dataSource     = self;
    _tableView.delegate       = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    _addFoodButton = [[UIButton alloc] init];
    _addFoodButton.frame = SNFRAME(50, _tableView.centerY-20,HHHWIDTH-100, 40);
    [_addFoodButton setTitle:SN_Text(@"点击右上角图标以创建记录") forState:UIControlStateNormal];
//    [_addFoodButton addTarget:self action:@selector(functionSetRight) forControlEvents:UIControlEventTouchUpInside];
    _addFoodButton.titleLabel.lineBreakMode = 0;
    _addFoodButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    [_addFoodButton setTitleColor:FOODCOLOR forState:UIControlStateNormal];
    [self.view addSubview:_addFoodButton];
    
    _resultView                   = [[SNShadowView alloc] initWithFrame:SNFRAME(10, _tableView.bottom, HHHWIDTH-20, HHHHEIGHT - _tableView.bottom -30)];
    _resultView.layer.borderWidth = 0.5;
    _resultView.layer.borderColor  = SNCOLORGRAYLINE.CGColor;
    _resultView.layer.cornerRadius = 5.0;
    [self.view addSubview:_resultView];
    
    _foodInfoLabel = [UILabel simpleWithRect:SNFRAME(5,_resultView.height/4.0, _resultView.width-10, 20) withAlignment:NSTextAlignmentCenter withFontSize:13.0 withText:@"" withTextColor:[UIColor grayColor]];
    _foodInfoLabel.adjustsFontSizeToFitWidth = YES;
    [_resultView addSubview:_foodInfoLabel];
    
    _segLineView = [[SNFoodSegLineView alloc] initWithFrame:SNFRAME(5, _foodInfoLabel.bottom + 10, _resultView.width -10, 40)];
    [_resultView addSubview:_segLineView];
    
    _foodMaxLabel = [UILabel simpleWithRect:SNFRAME(0,_resultView.height -25, _resultView.width, 20) withAlignment:NSTextAlignmentCenter withFontSize:13.0 withText:@"" withTextColor:[UIColor grayColor]];
    [_resultView addSubview:_foodMaxLabel];
    _foodMaxLabel.adjustsFontSizeToFitWidth = YES;
    
    [self updateFoodLabel];
}

- (void)updateFoodLabel {
    NSInteger baseBmr  = SN_PublicClass.todayFood.goal_calory;
    NSInteger min          = baseBmr*1.05;
    NSInteger max          = baseBmr*1.1;
    NSString  *calorString = [NSString stringWithFormat:@"%d-%d", min, max];
    NSString  *infoString  = [NSString stringWithFormat:@"%@ %@ %@", SN_Text(@"您每天最多摄入约"), calorString, SN_Text(@"大卡")];
    _foodInfoLabel.text = infoString;
    [_foodInfoLabel setAttributedText:[SN_PublicClass labelSelfText:_foodInfoLabel.text withChangeText:calorString withFontsize:15 withChangeColor:[UIColor blackColor]]];

    NSInteger totalColor = SN_PublicClass.todayFood.currentCalor;
    NSString * colorTotalString = StrByInt(totalColor);
    NSString *string = [NSString stringWithFormat:@"%@ %@ %@",SN_Text(@"当前摄入约"),colorTotalString,SN_Text(@"大卡")];
    _foodMaxLabel.text = string;
    if (totalColor > baseBmr) {
        [_foodMaxLabel setAttributedText:[SN_PublicClass labelSelfText:_foodMaxLabel.text withChangeText:colorTotalString withFontsize:18.0 withChangeColor:FOODCOLOR]];
    }
    else
    {
        [_foodMaxLabel setAttributedText:[SN_PublicClass labelSelfText:_foodMaxLabel.text withChangeText:colorTotalString withFontsize:18.0 withChangeColor:[UIColor blackColor]]];
    }
    
    NSMutableArray *calorArray = [NSMutableArray array];
    for (FoodEatModel *model in SN_PublicClass.todayFood.array) {
        [calorArray addObject:[NSString stringWithFormat:@"%.0f",model.caloryTotal]];
    }
      [_segLineView updateView:calorArray withTotal:max];
    
    [self updateRightButton];
}

- (void)functionSetLeft {
    [self.navigationController popViewControllerAnimated:YES];
}

// 新增用餐
- (void)functionSetRight {
    SNFoodEditViewController *vc = [[SNFoodEditViewController alloc] init];
    vc.add = YES;
    FoodEatModel *model = [[FoodEatModel alloc] init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

// 拉取当日最新食谱
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self updateFoodList];
}

- (void)updateRightButton
{
    NSInteger count = SN_PublicClass.todayFood.count;
    if (count <999) {
        self.rightButton.hidden = NO;
        [self.rightButton setImageNormal:@"添加加号无边框"];
    }
    else
    {
        self.rightButton.hidden = YES;
        [self.rightButton setImageNormal:@"添加加号无边框"];
    }
    
    if (count == 0) {
        _addFoodButton.hidden = NO;
    }
    else
    {
        _addFoodButton.hidden = YES;
    }
}

- (void)updateFoodList
{
    NSString *todayString = SN_PublicClass.todayFood.dateString;
    DEF_WEAKSELF_(SNFoodViewController)
    [SN_Net foodListSearchWithDate: todayString Success:^(id object) {
        [weakSelf updateWithArray:object with:todayString];
    } fail:^(id object) {
    }];
}

- (void)updateWithArray:(id)object with:(NSString *)dateString{
    NSDictionary *dic = object[@"data"];
    [FoodModel updateModelWithDic:dic withDateString:dateString];
    
    _dataArray = [NSMutableArray array];
    [_dataArray addObjectsFromArray:SN_PublicClass.todayFood.array];
    [_tableView reloadData];
    
    [self updateFoodLabel];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString         *identifier = @"SNFoodMealTableViewCell";
    SNFoodMealTableViewCell *cell       = [tableView cellForRowAtIndexPath:indexPath];
    
    if (nil == cell) {
        cell = [[SNFoodMealTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    FoodEatModel *model = [_dataArray objectAtIndex:indexPath.row];
    [cell updateCellWithModel:model];
    
//    背景颜色
    UIView *tempView1 = [[UIView alloc] initWithFrame:cell.frame];
    tempView1.backgroundColor   = [UIColor grayColor];
    tempView1.alpha = 0.05;
    cell.selectedBackgroundView = tempView1;
    //    禁止cell选中
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [_segLineView selectView:indexPath.row];
//    SNFoodEditViewController *vc = [[SNFoodEditViewController alloc] init];
//    vc.add = NO;
//    FoodEatModel *model = [_dataArray objectAtIndex:indexPath.row];
//    vc.model = model;
//    [self.navigationController pushViewController:vc animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:SN_Text(@"编辑")  handler:^(UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath) {
        
        SNFoodEditViewController *vc = [[SNFoodEditViewController alloc] init];
        vc.add = NO;
        FoodEatModel *model = [_dataArray objectAtIndex:indexPath.row];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    }];
    
    editAction.backgroundColor = FOODCOLOR;
    
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:SN_Text(@"删除")  handler:^(UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath) {
        FoodEatModel *model = [_dataArray objectAtIndex:indexPath.row];
        [SN_Net showHUD:SN_Text(@"请等待...") withTime:10.0];
        [SN_Net FoodDeleteWithFoodModel:model Success:^(id object) {
            [self updateFoodList];
        } fail:^(id object) {
            
        }];
        [tableView reloadData];
    }];
    
    deleteAction.backgroundColor = SNCOLORBLACK;
    
    return @[deleteAction,editAction];
}

@end
